using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using MyClever.Lib;

namespace MyCleverWizard
{
    public partial class Form1 : Form
    {
        private List<Control> subPages = new List<Control>();

        private int currentPage = 0;

        private string missionFilePath;
        private Mission mission;

        #region Properties

        public Mission MissionReference
        {
            get
            {
                return this.mission;
            }
        }

        public bool EnableFinishButton
        {
            get
            {
                return this.btnToStart.Enabled;
            }
            set
            {
                this.btnToStart.Enabled = value;
            }
        }
        #endregion

        /// <summary>
        /// Initializes a new instance of the <see cref="Wizard"/> class.
        /// </summary>
        public Form1(string missionFilePath)
        {
            InitializeComponent();

            this.missionFilePath = missionFilePath;
        }

        public void AddPage(Control page)
        {
            this.subPages.Add(page);
            
            this.ResizeDialog();
        }

        private void ShowPage()
        {
            Debug.Assert(this.subPages.Count > 0, "There must be at least on page.");

            // Show the Page
            Control currentPage = this.subPages[this.currentPage];
            if (currentPage != null)
            {
                if (false == this.pnlSubPage.Controls.Contains(currentPage))
                {
                    this.pnlSubPage.Controls.Clear();
                    this.pnlSubPage.Controls.Add(currentPage);
                    currentPage.Dock = DockStyle.Fill;
                }
            }

            this.Text = string.Format("MyCleverWizard ({0} of {1})", this.currentPage + 1, this.subPages.Count);
            this.EnableActiveButtons();
        }


        private void ResizeDialog()
        {
            // Go through each page in the group and resize the containing dialog
            foreach (Control subPageOn in this.subPages)
            {
                Size size = this.ClientSize;
                if (size.Width < subPageOn.Width)
                {
                    size.Width = subPageOn.Width;
                    this.pnlSubPage.Width = subPageOn.Width;
                }
                if (size.Height < subPageOn.Height + this.pnlNavigation.Height)
                {
                    size.Height = subPageOn.Height + this.pnlNavigation.Height;
                }
                this.ClientSize = size;
            }
        }

        private void EnableActiveButtons()
        {
            this.btnBack.Enabled = (this.currentPage != 0);
            this.btnNext.Enabled = (this.currentPage + 1 != this.subPages.Count);

        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            this.currentPage--;
            ShowPage();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            this.currentPage++;
            ShowPage();
        }

        private void btnToStart_Click(object sender, EventArgs e)
        {
            //select last sheet, which should be the ProgressSheet
            this.currentPage = this.subPages.Count - 1;
            ShowPage();

            ProgressSheet progressSheet = (ProgressSheet)this.subPages[this.currentPage];
            progressSheet.StartMission();
        }

        private void Wizard_Load(object sender, EventArgs e)
        {
            if (true == String.IsNullOrEmpty(this.missionFilePath))
            {
                this.openFileDialog1.Filter = "Mission files (*.xml)|*.xml|All files (*.*)|*.*";
                this.openFileDialog1.InitialDirectory = ".\\Missions\\";
                
                if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    this.missionFilePath = this.openFileDialog1.FileName;
                }
            }
            
            mission = new Mission(this.missionFilePath);

            this.AddPage(new InfoSheet());

            foreach (MissionPluginProxy pluginOn in this.mission.PluginCollection.GetRoot().GetPlugins(true))
            {
                foreach (MissionPluginPackage packageOn in pluginOn.GetPackages(Direction.Input))
                {
                    if (false == packageOn.HasReference && 0 == packageOn.GetColumnsWithColumnReference().Count)
                    {
                        this.AddPage(new PackageSheet(packageOn));
                    }
                }
            }
            this.AddPage(new ProgressSheet());

            this.ShowPage();
        }
    }
}